package com.controller;

import com.constant.Constant;
import com.entity.User;
import com.exception.ServiceException;
import com.github.pagehelper.PageInfo;
import com.service.UserService;
import com.util.AjaxResult;
import com.vo.UserBackVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * date:2021/5/6
 * description
 * Author:lfz
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/loginBack.do")
    @ResponseBody
    public AjaxResult loginBack(HttpServletRequest request, User u) {
        AjaxResult result = new AjaxResult();
        String code =  request.getParameter("code");
        String localCode = (String) request.getSession().getAttribute("code");
        if(localCode ==null || !localCode.equals(code)){
            result.setMsg("验证码错误");
            //return result;
        }

        System.out.println("controller:"+u);
        try {
            User user = userService.loginBack(u);
            request.getSession().setAttribute(Constant.SESSION_USER, user);
            result.setSuccess(true);
        }catch (ServiceException e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }


    @RequestMapping("/findBackUser.do")
    @ResponseBody
    public AjaxResult findBackUser(HttpServletRequest request)
    {
        AjaxResult result = new AjaxResult(true, "查询成功", null);
        UserBackVo userBackVo=new UserBackVo();

        Integer pageNo = Integer.valueOf(request.getParameter("pageNo"));

        userBackVo.setEmail(request.getParameter("email"));
        userBackVo.setLogin_name(request.getParameter("login_name"));
        userBackVo.setNickname(request.getParameter("nickname"));
        userBackVo.setRole(request.getParameter("role"));
        userBackVo.setCreate_start_date(request.getParameter("create_start_date"));
        userBackVo.setCreate_end_date(request.getParameter("create_end_date"));
        userBackVo.setLogin_end_date(request.getParameter("login_start_date"));
        userBackVo.setLogin_start_date(request.getParameter("login_end_date"));

        PageInfo<UserBackVo> info = userService.findBackUser(userBackVo,pageNo);
        result.setObj(info);

        return result;
    }

//    modifyBackUser.do
//    user/showBackUser.do

    @RequestMapping("/showBackUser.do")
    @ResponseBody
    public User showBackUser(HttpServletRequest request){

        Integer id=Integer.valueOf(request.getParameter("id"));
        userService.showBackUser(id);
        return userService.showBackUser(id);
    }


    @RequestMapping("/modifyBackUser.do")
    @ResponseBody
    public void modifyBackUser(HttpServletRequest request){

        User user=new User();
        Integer id= Integer.valueOf(request.getParameter("id"));
        Integer status= Integer.valueOf(request.getParameter("status"));
        user.setId(id);
        user.setStatus(status);

        user.setEmail(request.getParameter("email"));
        user.setNickname(request.getParameter("nickname"));
        user.setPassword(request.getParameter("password"));
        user.setRole(request.getParameter("role"));
        userService.modifyBackUser(user);
    }

}
